package com.carsite.webapp.help;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.carsite.model.ModuleMenu;

public class ModuleMenuHelp {
	
	
	public static String dumpModuleMenuScript(List menus,String isParent,String isDiv) {
		String scriptStr = "";
		if("true".equals(isDiv)){
			 scriptStr = "var menuTree = new WebFXTree('选择菜单');\r\n";
			 
			 if("true".equals(isParent)){
		     	scriptStr += "var area_root_item = new WebFXTreeItem('置空','javascript:onSelMenu(0,0)');\r\n";
		     }else{
		     	scriptStr += "var area_root_item = new WebFXTreeItem('置空','javascript:onSelMenu(0,0)');\r\n";
		     }
			 scriptStr += "menuTree.add(area_root_item);\r\n";
			 scriptStr = scriptStr+"menuTree.setBehavior('explorer');\r\n";  	     	 
		}else{
		     scriptStr = "var menuTree = new WebFXTree('模块菜单');\r\n";
		     scriptStr = scriptStr+"menuTree.setBehavior('explorer');\r\n";
		}
		
		Iterator it = menus.iterator();
	    return scriptStr+makeMenuTree("menuTree",it);
	}
	
	 
	
	  private static String makeMenuTree(String parentName,Iterator it){
	  	String scriptStr ="";
	  
	  	while(it.hasNext()){
	           ModuleMenu menu = (ModuleMenu)it.next();
	           String name =  menu.getMenuName();
	           Long menuId = menu.getMenuId();
	           if("menuTree".equals(parentName)){
	        	   scriptStr = scriptStr+"var item_"+name+" = new WebFXTreeItem('"+name+"','javascript:onSelMenu("+menuId+",1)');\r\n";
	        	   scriptStr = scriptStr+ "item_"+name+".icon =  'tree/foldericon.png';\r\n";
	        	   scriptStr = scriptStr+ "item_"+name+".openIcon =  'tree/openfoldericon.png';\r\n";
	        	     
	   	           
	           }else{
	        	   scriptStr = scriptStr+"var item_"+name+" = new WebFXTreeItem('"+name+"','javascript:onSelMenu("+menuId+",0)');\r\n"; 
	        	   scriptStr = scriptStr+ "item_"+name+".icon = 'tree/file.png';\r\n";
	           }
	           
	           if(menu.getChildren().size()>0){
	            Iterator itOne = menu.getChildren().iterator();
	            scriptStr = scriptStr+ makeMenuTree("item_"+name,itOne);
	        }
	           scriptStr = scriptStr+ parentName+".add(item_"+name+");\r\n";
        } 
	  	return scriptStr;
     }
	  
	  public static String dumpPrivilegeMenuScript(List menus,Map selectedMenu,boolean disable) {
			
		  String scriptStr = "";
		  scriptStr = "var menuTree = new WebFXTree('勾选权限菜单');\r\n";
		  scriptStr = scriptStr+"menuTree.setBehavior('explorer');\r\n";  	     	 
		  Iterator it = menus.iterator();
		  return scriptStr+makePrivilegeMenuTree(it,selectedMenu,"menuTree",disable);
		    
	  }
		
	  private static String makePrivilegeMenuTree(Iterator it,Map selectedMap,String parentName,boolean disable){
		  String scriptStr ="";
		  
		  	while(it.hasNext()){
		           ModuleMenu menu = (ModuleMenu)it.next();
		           String name =  menu.getMenuName();
		           Long menuId = menu.getMenuId();
		           
		           String tmpName = name +"-"+ menuId;
		           
		           boolean checked=false;
		           if(selectedMap.containsKey(menuId)){
		        	   checked = true;
		           }
		           
		           if("menuTree".equals(parentName)){
		        	   scriptStr = scriptStr+"var item_"+name+" = new WebFXCheckBoxTreeItem('"+tmpName+"','',"+checked+");\r\n";
		        	   scriptStr = scriptStr+ "item_"+name+".icon =  'tree/foldericon.png';\r\n";
		        	   scriptStr = scriptStr+ "item_"+name+".openIcon =  'tree/openfoldericon.png';\r\n";
		        	     
		   	           
		           }else{
		        	   scriptStr = scriptStr+"var item_"+name+" = new WebFXCheckBoxTreeItem('"+tmpName+"','',"+checked+");\r\n"; 
		        	   scriptStr = scriptStr+ "item_"+name+".icon = 'tree/file.png';\r\n";
		           }
		           
		           if(menu.getChildren().size()>0){
		             Iterator itOne = menu.getChildren().iterator();
		             scriptStr = scriptStr+ makePrivilegeMenuTree(itOne,selectedMap,"item_"+name,disable);
		           }
		           scriptStr = scriptStr+ parentName+".add(item_"+name+");\r\n";
	        } 
		  	return scriptStr;
     }
	  
}
